<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../node_modules/vue/dist/vue.global.js"></script>
</head>

<body>
    <div id="app">
        <ul>
            <!-- 当给li 设置唯一索引,这样选中的项永远不变,否则改变 -->
            <li v-for="item in hreoArr" :id="item.id">
                <input type="checkbox" /> {{item.name}}---- {{item.type}}
            </li>
        </ul>
        <p>
            <input type="text" id="hero">
            <input type="text" id="heroType">
            <button @click="addAheroFn">添加英雄</button>
        </p>
    </div>
</body>

</html>
<script>
    const app = Vue.createApp({
        data() {
            return {
                hreoArr: [
                    {
                        id: 1,
                        name: "典韦",
                        type: '战士'
                    },
                    {
                        id: 2,
                        name: "李白",
                        type: '刺客'
                    },
                    {
                        id: 3,
                        name: "诸葛亮",
                        type: '刺法师客'
                    }
                ],
                hero: '孙膑',
                herotype: '辅助'
            }
        },
        methods: {
            addAheroFn() {
                // console.log(app === this); 都指向当前的实例对象 app
                // console.log(this);
                // console.log(this.hero, this.herotype);
                this.hreoArr.unshift({
                    id: this.hreoArr[0].id - 1,
                    name: document.querySelector('#hero').value,
                    type: document.querySelector('#heroType').value
                })
            }
        }
    }).mount('#app')

</script>